EEL_MODS	+= buildroot

PHONY-buildroot += br-%
br-%: $(BUILD_DEPS) $(O_BR_DIR) $(RFS_DEP_br)
	$(EELQ2)$(MAKE) \
		-C $(S_BR_DIR) \
		ARCH=$(BR_ARCH) \
		O=$(O_BR_DIR) \
		BUSYBOX_OVERRIDE_SRCDIR=$(S_BB_DIR) \
		LINUX_OVERRIDE_SRCDIR=$(S_KL_DIR) \
		LINUX_OVERRIDE_SRCDIR_RSYNC_EXCLUSIONS="$(RSYNC_OPTS_EXCLUDE) --exclude $(S_KL_DIR)/Documentation" \
		BR2_LINUX_KERNEL_DEFCONFIG=$(subst _defconfig,,$(KL_CONFIG)) \
		BR2_ROOTFS_OVERLAY="$(RFS_BR_OVERLAY)" \
		$(patsubst br-%,%,$@)

$(eval $(call deftargets_git,buildroot,BR))
$(eval $(call deftargets_download,buildroot,BR,$$(BR_PKG),$$(BR_VERSION)))

PHONY-buildroot += buildroot
buildroot: $(BUILD_DEPS)
buildroot: $(O_BR_DIR)
buildroot: $(RFS_DEP_br)
# ARM_TEE=1
buildroot: $(RFS_TEE_DEPS)
# use custom linux kernel path
ifeq ($(BR_KL_ENABLE),1)
buildroot: kernel-prepare
endif
# use custom busybox path
buildroot: busybox-prepare
buildroot: buildroot-prepare
	$(EELQ3)$(MAKE_THIS) buildroot-defconfig

	$(EELQ2)$(MAKE) \
		-C $(S_BR_DIR) \
		ARCH=$(BR_ARCH) \
		O=$(O_BR_DIR) \
		BUSYBOX_OVERRIDE_SRCDIR=$(S_BB_DIR) \
		LINUX_OVERRIDE_SRCDIR=$(S_KL_DIR) \
		LINUX_OVERRIDE_SRCDIR_RSYNC_EXCLUSIONS="$(RSYNC_OPTS_EXCLUDE) --exclude $(S_KL_DIR)/Documentation" \
		BR2_LINUX_KERNEL_DEFCONFIG=$(subst _defconfig,,$(KL_CONFIG)) \
		BR2_ROOTFS_OVERLAY="$(RFS_BR_OVERLAY)" \
		$(BR_TARGETS)

PHONY-buildroot += buildroot-defconfig buildroot-modifyconfig buildroot-menuconfig buildroot-savedefconfig
buildroot-defconfig: buildroot-prepare
	$(EELQ5)$(MAKE_THIS) buildroot-modifyconfig
	$(EELQ5)$(MAKE_THIS) br-$(BR_CONFIG)

# we only export EEL_xx_DIR
buildroot-modifyconfig:
	$(EELQ)sed -i 's%BR2_DL_DIR=.*%BR2_DL_DIR="$$(EEL_DL_DIR)/buildroot-dl"%' \
		$(S_BR_DIR)/configs/$(BR_CONFIG)
	$(EELQ)sed -i 's%BR2_TOOLCHAIN_EXTERNAL_PATH=.*%BR2_TOOLCHAIN_EXTERNAL_PATH="$$(EEL_TC_DIR)/$(TC_BASE_OS)"%' \
		$(S_BR_DIR)/configs/$(BR_CONFIG)
	$(EELQ)sed -i 's%BR2_TOOLCHAIN_EXTERNAL_CUSTOM_PREFIX=.*%BR2_TOOLCHAIN_EXTERNAL_CUSTOM_PREFIX="$(TC_PREFIX_BASE)"%' \
		$(S_BR_DIR)/configs/$(BR_CONFIG)
	$(EELQ)sed -i 's%BR2_TOOLCHAIN_EXTERNAL_GCC_[0-9]*=y%BR2_TOOLCHAIN_EXTERNAL_GCC_$(firstword $(subst ., ,$(TC_VERSION_OS)))=y%' \
		$(S_BR_DIR)/configs/$(BR_CONFIG)

buildroot-menuconfig: br-menuconfig

buildroot-savedefconfig: br-savedefconfig
	$(EELQ5)mkdir -p $(EEL_CHG_DIR)/$(BR_VER_DIR)/configs
	$(EELQ)cp $(S_BR_DIR)/configs/$(BR_CONFIG) \
		$(EEL_CHG_DIR)/$(BR_VER_DIR)/configs/$(BR_SAVECONF)

$(O_BR_DIR):
	mkdir -p $@

